package com.example.module_visitor.view

import android.annotation.SuppressLint
import android.app.DatePickerDialog
import android.app.TimePickerDialog
import android.content.Intent
import android.widget.Toast
import com.alibaba.android.arouter.facade.annotation.Route
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.blankj.utilcode.util.ToastUtils
import com.bumptech.glide.Glide
import com.example.module_visitor.databinding.ActivityAddVisitorBinding
import com.example.module_visitor.viewmodel.VisitorVIewModel
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import com.yuwei.base.BaseActivity
import com.yuwei.common.path.RouterPath
import com.yuwei.http.entity.AddAppointmentsBody
import dagger.hilt.android.AndroidEntryPoint
import java.util.*


@AndroidEntryPoint
@Route(path = RouterPath.ADD_APPOINTMENTS_URL)
class AddVisitorActivity : BaseActivity<ActivityAddVisitorBinding,VisitorVIewModel>() {
    var imagePath = mutableListOf<String>()
    @SuppressLint("SetTextI18n")
    override fun initView() {
        binding.addVisitDataView.setOnClickListener {
            DatePickerDialog(this,
                { view, year, month, dayOfMonth ->
                    binding.addVisitData.text = "$year-${month+1}-$dayOfMonth"
                }, 2021, 1, 1).show()
        }
        binding.addVisitStartTime.setOnClickListener {
            TimePickerDialog(this,
                { view, hourOfDay, minute ->
                    binding.addVisitStartTime.text = "$hourOfDay:$minute"
                }, 10, 10, true).show()
        }
        binding.addVisitEndTime.setOnClickListener {
            TimePickerDialog(this,
                { view, hourOfDay, minute ->
                    binding.addVisitEndTime.text = "$hourOfDay:$minute"
                }, 10, 10, true).show()
        }
        binding.visitBack.setOnClickListener {

            finish()
        }
        binding.addVisitCarNum.isEnabled = false
        binding.addVisitParkingSwitch.setOnClickListener {
             it.isSelected =!it.isSelected
            binding.addVisitCarNum.isEnabled = it.isSelected
            if (it.isSelected){
               visitorCp()
            }
        }
        binding.addVisitCarNum.setOnClickListener {
            visitorCp()
        }
        binding.addVisitLoadFile.setOnClickListener {
            PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()

        }
//        kingKeyboard.hide()
        binding.addVisitorBtn.setOnClickListener {

           val addAppointmentsBody = AddAppointmentsBody(
               appointmentTime =  "${ binding.addVisitData.text } ${binding.addVisitStartTime.text}",
               approved = true,
               attachments = imagePath,
               employeeId = 1,
               id = 0,
               idNumber = binding.addVisitCardNum.text.toString().toLong(),
               phoneNumber = binding.addVisitPhoneNum.text.toString().toLong(),
               plateNumber = binding.addVisitCarNum.text.toString(),
               purpose = binding.addVisitPurpose.text.toString(),
               requestParking = true,
               visitStatus = "待审核",
               visitorName = binding.addVisitName.text.toString()
           )
            vm.getAddVisitor(addAppointmentsBody)

        }
    }

    override fun initData() {
        vm.addVisitorSuccess.observe(this){
            ToastUtils.showLong("保存成功")
            finish()
        }
    }
    fun visitorCp(){
        val provinces = mutableListOf(
            "京", "沪", "粤", "津", "冀", "晋", "蒙", "辽", "吉", "黑",
            "苏", "浙", "皖", "闽", "赣", "鲁", "豫", "鄂", "湘", "桂",
            "琼", "渝", "川", "贵", "云", "藏", "陕", "甘", "青", "宁", "新"
        )
        val provinces1= ArrayList<String>()
        provinces.forEach {
            provinces1.add(it)
        }
        val letters = mutableListOf(
            "A", "B", "C", "D", "E", "F", "G", "H", "J", "K",
            "L", "M", "N", "P", "Q", "R", "S", "T", "U", "V",
            "W", "X", "Y", "Z"
        )
        val letters1= ArrayList<String>()
        letters.forEach {
            letters1.add(it)
        }
        // 修改list初始化逻辑，为每个省份创建对应的二级数据
        val list = mutableListOf<List<String>>().apply {
            // 为每个省份添加相同的字母列表（共31个二级列表）
            repeat(provinces1.size) {
                add(letters1)
            }
        }
        val pickerView = OptionsPickerBuilder(
            this
        ) { options1, options2, options3, v ->
            binding.addVisitCarNum.setText("${provinces1.get(options1)}${list.get(options1).get(options2)}·")
        }.build<String>()
        pickerView.setPicker(provinces1,list);
        pickerView.show()
    }
    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if (requestCode == PictureSelector.SELECT_REQUEST_CODE && resultCode == RESULT_OK) {
            val bundle = data?.extras
            val pictureBean = bundle?.get(PictureSelector.PICTURE_RESULT)as? PictureBean
            pictureBean?.path?.let { path->
                imagePath.add(path)
                Glide.with(this).load(path).into(binding.addVisitLoadFile)
            }
        }

    }
}